﻿/**
* @file         wit_motion_protocol.h
* @author       David Hu (hmd_hubei_cn@163.com)
* @brief        基于维特智能WIT私有协议创建
* @version      1.0
* @date         2023.09.26
* @note         https://wit-motion.yuque.com/wumwnr/ltst03/vl3tpy#O2frB
* @copyright    Copyright (c) 2022 DAVID HU All rights reserved. Licensed under the MIT License (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License in the file LICENSE

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.
**/

#ifndef __WIT_MOTION_PROTOCOL_H__
#define __WIT_MOTION_PROTOCOL_H__

#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#ifdef __cplusplus
extern "C"{
#endif

// -- 通信协议
#define WIT_PKG_HEADER                                    0x55  /* 协议头 */
#define WIT_READ_FRAME_LEN                                11
#define WIT_READ_FRAME_DATAS_LEN                          8
#define WIT_PKG_UNPACK_LEN                                100  /* 解包，数据区buffer大小 */
#define WIT_PKG_PACK_LEN                                  100  /* 打包，数据区buffer大小 */

#define WIT_PKG_ID_ACC_OUT                                0x51  // 三轴加速度
#define WIT_PKG_ID_ANGULARVEL_OUT                         0x52  // 三轴角速度
#define WIT_PKG_ID_ANGULAR_OUT                            0x53  // 三轴角度
#define WIT_PKG_ID_LON_LAT_OUT                            0x57  // 经纬度
#define WIT_PKG_ID_GPS_DATA_OUT                           0x58  // GPS数据

typedef enum {
  WIT_HEADER = 0,
  WIT_ID,
  WIT_DATA
} wit_protocol_state_t;

typedef struct {
  wit_protocol_state_t state;
  uint16_t             package_len;
  uint16_t             package_index;
  uint8_t              data_id;
  uint8_t              *data_in_buff;
  uint32_t             data_in_len;
  uint8_t              *data_out_buff;
  uint32_t             *data_out_len;
} wit_packdata_t;

#pragma pack(1)   // 取消默认对齐，按单字节对齐
typedef struct {
  int16_t acc_x;
  int16_t acc_y;
  int16_t acc_z;
  uint16_t temper;
} imu_acc_msg_t;

typedef struct {
  int16_t w_x;
  int16_t w_y;
  int16_t w_z;
  uint16_t voltage;
} imu_angularvel_msg_t;

typedef struct {
  int16_t roll;
  int16_t pitch;
  int16_t yaw;
  union {
    uint16_t vernum;
    struct {
      uint8_t vl;
      uint8_t vh;
    };
  };
} imu_angular_msg_t;

typedef struct {
  union {
    int32_t lonval;
    uint8_t lon[4];
  };
  union {
    int32_t latval;
    uint8_t lat[4];
  };
} gps_longitude_latitude_msg_t;

typedef struct {
  uint8_t gps_height_lsb;
  uint8_t gps_height_msb;
  uint8_t gps_yaw_lsb;
  uint8_t gps_yaw_msb;
  uint8_t gps_speed[4];
} gps_base_data_msg_t;

#pragma pack()   // 恢复默认对齐


bool wit_unpack(uint8_t in_data, wit_packdata_t *out_package);


#ifdef __cplusplus
}
#endif


#endif //__WIT_MOTION_PROTOCOL_H__
/********************* (C) COPYRIGHT DAVID HU *******END OF FILE ********/
